import React, { PureComponent, Fragment } from 'react';
import { Table } from 'antd';
import styles from './index.less';

class GeneralTableComponent extends PureComponent {
  constructor(props) {
    super(props);
    const { columns } = props;
    this.state = {};
  }

  componentWillReceiveProps(nextProps) {}
  handleTableChange = (pagination, filters, sorter) => {
    const { onChange } = this.props;
    onChange(pagination, filters, sorter);
  };

  render() {
    const {
      data: { list, pagination },
      loading,
      columns,
      handleRowClass,
      showSizeChanger,
      rowKey,
    } = this.props;

    let keyList = [];
    if (list) {
      list.forEach((val, index, arr) => {
        if (typeof(val) === 'object') {
          for (let [_key, _val] of Object.entries(val)) {
            if (!_val && _val != 0 && typeof(_val) !== 'undefined') {
              val[_key] = '/';
            }
          }
        }
      });
      keyList = list[0] ? Object.keys(list[0]).map(key => key) : [];
    }
    let _rowKey = rowKey && list && list[0] ? rowKey : keyList.find(val =>
      val.includes('id') ||
      val.includes('Id') ||
      val.includes('ID') ||
      val.includes('no') ||
      val.includes('No') ||
      val.includes('NO') ||
      val.includes('date')
    );
    //在没有设置rowKey时,默认选择带有id || Id || ID || no || No || NO || date字符的键名作为rowKey;
    const paginationProps = {
      showSizeChanger: showSizeChanger,
      showQuickJumper: true,
      ...pagination,
    };

    return (
      <div className={styles.standardTable}>
        <Table
          loading={loading}
          dataSource={list}
          columns={columns}
          pagination={paginationProps}
          onChange={this.handleTableChange}
          rowClassName={handleRowClass}
          rowKey={record => record[_rowKey]}
        />
      </div>
    );
  }
}

export default GeneralTableComponent;
